﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using Easy.Common;
using Easy.Plugin.Interface;
using Microsoft.Practices.ServiceLocation;

namespace Easy.Plugin.Managers
{
    class DefaultPluginManager : PluginRootManager
    {
        [ImportMany]
        IEnumerable<Lazy<IDefaultPlugin>> _defaultPlugins;
        private PluginRootManager _pluginManager;


        public DefaultPluginManager(PluginRootManager pluginManager)
            : base(pluginManager.PluginData)
        {
            _pluginManager = pluginManager;
        }

        public override void Run()
        {
            RunParent();

            RunMe();
        }


        #region private

        private void RunParent()
        {
            _pluginManager.Run();

            _pluginManager.Container.ComposeParts(this);

            base.Container = _pluginManager.Container;
        }

        private void RunMe()
        {
            if (_defaultPlugins == null)
            {
                return;
            }

            foreach (var plugin in _defaultPlugins)
            {
                plugin.Value.Run();
            }
        }

        #endregion
    }
}
